﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace GOM_PLAYER.control_subtitle
{
    public class NodeSub
    {
        public const string TIME = "time";
        public const string CONTEXT ="context";
        private string time;
        private string context;

        public NodeSub()
        {
            this.time = "";
            this.context = "";
        }
        public NodeSub(string time, string context)
        {
            this.time = time;
            this.context = context;
        }
        public string Time
        {
            get { return time; }
            set { time = value; }
        }

        public string Context
        {
            get { return context; }
            set { context = value; }
        }
    }
    public class Subtitle 
    {
        private List<NodeSub> elements;

        
        public List<NodeSub> Elements
        {
            get { return elements; }
            set { elements = value; }
        }

        public Subtitle()
        {

            this.elements = new List<NodeSub>();
        }

        public void addElement(NodeSub element)
        {
            if (this.elements == null)
            {
                this.elements = new List<NodeSub>();
            }
            this.elements.Add(element);
        }

        public static Subtitle readXMLSubtitle(string url)
        {
            Subtitle result = new Subtitle();
            try
            {

                XmlTextReader reader = new XmlTextReader(url); 
                NodeSub node = new NodeSub();
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name == NodeSub.TIME && node != null)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    node.Time = reader.Value;
                                }
                            }
                            if (reader.Name == NodeSub.CONTEXT)
                            {
                                reader.Read();
                                if (reader.NodeType == XmlNodeType.Text)
                                {
                                    node.Context = reader.Value;
                                }
                                result.addElement(node);
                                node = new NodeSub();
                            }
                            break;
                    }
                }
                reader.Close();

            }
            catch (Exception ex)
            {

            }

            return result;

        }

    }
}
